﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WarningAndMonitingSystem.Controls
{
    public partial class UAlarm : UserControl
    {
        private Rectangle workArea;
        private System.Timers.Timer timer;

        public UAlarm()
        {
            InitializeComponent();
            SetStyle(ControlStyles.AllPaintingInWmPaint,true);
            SetStyle(ControlStyles.UserPaint,true);
            SetStyle(ControlStyles.ResizeRedraw,true);
            SetStyle(ControlStyles.SupportsTransparentBackColor,true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer,true);
            SetStyle(ControlStyles.Selectable, true);

            this.SizeChanged += UAlarm_SizeChanged;
            this.Size = new Size(50, 50);

            // 初始化定时器
            timer = new System.Timers.Timer();
            timer.AutoReset = true;
            timer.Interval = 200;
            timer.Elapsed += Timer_Elapsed;
        }

        private int colorIdx = 0;

        private void Timer_Elapsed(object? sender, System.Timers.ElapsedEventArgs e)
        {
            this.Invoke(new Action(() =>
            {
                colorIdx++;
                if (colorIdx >= lightColors.Length)
                {
                    colorIdx = 0;
                }
                Invalidate();
            }));
        }

        private void UAlarm_SizeChanged(object? sender, EventArgs e)
        {
            workArea = new Rectangle(Width / 8,Height / 8, Width / 4 *3,Height / 4 * 3);
        }

        private Color standColor = Color.DarkGray;
        [Category("自定义属性")]
        [Description("底座颜色")]
        public Color StandColor
        {
            get { return standColor; }
            set { 
                standColor = value;
                Invalidate();
            }
        }

        private Color[] lightColors = new Color[2] { Color.Red,Color.Pink };
        [Category("自定义属性")]
        [Description("灯泡颜色")]
        public Color[] LightColors
        {
            get { return lightColors; }
            set { 
                if(value.Length < 2)
                {
                    MessageBox.Show("颜色列表长度至少为2");
                    return;
                }
                lightColors = value;
                Invalidate();
            }
        }

        private int interval = 200;
        [Category("自定义属性")]
        [Description("闪烁间隔")]
        public int Interval
        {
            get { return interval; }
            set { 
                if(value > 0)
                {
                    timer.Interval = value;
                    timer.Enabled = true;
                }
                else
                {
                    timer.Enabled = false;
                    MessageBox.Show("闪烁间隔不能小于0","错误",MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                interval = value;
            }
        }


        private bool isOn = false;
        [Category("自定义属性")]
        [Description("是否闪烁")]
        public bool IsOn
        {
            get { return isOn; }
            set { 
                isOn = value;
                if (isOn && interval > 0)
                {
                    timer.Start();
                }
                else
                {
                    timer.Stop();
                    colorIdx = 0;
                }

                Invalidate();
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.Clear(this.BackColor);

            GraphicsPath lightPath = GetLightPath(workArea);
            Color currentColor = lightColors[colorIdx];

            g.FillPath(new SolidBrush(currentColor), lightPath);

            Rectangle rect = ClientRectangle;
            int x = rect.X;
            int y = rect.Height / 4 * 3;
            int w = rect.Width;
            int h = rect.Height / 4;
            g.FillRectangle(new SolidBrush(standColor), new Rectangle(x, y, w, h));

        }

        private GraphicsPath GetLightPath(Rectangle rect)
        {
            int x = rect.X;
            int y = rect.Y;
            int w = rect.Width;
            int h = ClientRectangle.Height / 4 * 3;

            GraphicsPath path = new GraphicsPath();

            // 左边
            path.AddLine(x,h,x,y + w /2);
            // 上半圆
            path.AddArc(x,y,w,w,180f,180f);
            // 右边
            path.AddLine(x + w,y + w / 2, x + w, h);
            // 下边
            path.CloseAllFigures();

            return path;
        }
    }
}
